// plot.h: interface for the Plot class.
//
//////////////////////////////////////////////////////////////////////

#ifndef PLOT_H
#define PLOT_H

#include <qwidget.h>
#include <qcolor.h>
#include <qpixmap.h>
#include "datamapper.h"

class QPoint;

class Plot : public QObject
{
	Q_OBJECT
public:
	Plot(QWidget *parent = 0);

	void draw(QPainter &p);

	void	setName(const QString& name);
	QString getName();
	QPoint	getPointAtX(int x);
	QRect	getArea();
	void	setArea(QRect r);
    QColor	getLineColor();
    void	setLineColor(QColor color);

    void    setData(double *d, int size);
	void	updatePlot();
    void    enablePlot(bool enabled);

	void    setBaseLine(int pos);
	int		getBaseLine();	

	bool	zoom(const QPoint& p1, const QPoint& p2, bool saveZoom = false);
	bool	zoomIndex(int lowerIndex, int upperIndex, bool saveZoom = false);
	bool	zoomYScale(int lowerY, int upperY, bool saveZoom = false);
	void	zoomOut();
	void	resetZoom();
	bool	getZoom(int& maxRange, int& lowerIndex, int& upperIndex);
	int		getXRange();
	double	getYRange();
	double	getMaxYValue();
	double  getMinYValue();
	double	getMaxXValue();
	double  getMinXValue();
	int		getZoomLowerIndexX();
	int		getZoomUpperIndexX();
	int		getZoomLowerIndexY();
	int		getZoomUpperIndexY();

	bool	setXValueRange(double min, double max);
	bool	setYValueRange(double min, double max);

    void    resetYValues(); 
    void    setDrawMaxYValues(bool toggle);
    void    setDrawAverageYValues(bool toggle);

	double	indexToXValue(int index);
	int		xValueToIndex(double valueX);

    double * getMaxYValues();
    double * getAverageYValues();
    double getValueAtX(int x);
    QPoint getPointAtXRaw(int x);
    double getValueAtXRaw(int x);

	DataMapper* getDataMap();
signals:
    void    alarmEvent(int index, double value);

private:
	QString plotName;
	QPoint	calculatePlotPoint(int x, double y);
	bool	saveCurrentZoom();
	bool	loadZoom(int& lowerX, int& lowerY, int& upperX, int& upperY);
	
    bool	mEnabled;

	bool	validData;
	double*	dataBuffer;

    double * mMaxYValues;
    double * mAverageYValues;
    int mNumAverages;
    bool mDrawMaxYValues;
    bool mDrawAverageYValues;

	int		dataBufferSize;
	QRect	plotArea;
	QColor	lineColor;
	QPolygon	plotLine;
    QPolygon mMaxLine;
	bool	redraw;
    double     yRange;
	float	baseLine;

	/* These are the internal min and max */
	double	minY;
	double	maxY;

	/* These are the displayed min and max */
	double	minValueY;
	double	maxValueY;
	double	minValueX;
	double  maxValueX;
    DataMapper data;

	/* A list of zoom levels (odds are lower, evens are upper) */
	QList<int>	zoomList;
};

#endif
